package com.yuexun.beilunpatient.ui.my.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.umeng.message.UTrack;
import com.yuexun.beilunpatient.R;
import com.yuexun.beilunpatient.base.AppContext;
import com.yuexun.beilunpatient.base.BaseKJActivity;
import com.yuexun.beilunpatient.base.ExitApplication;
import com.yuexun.beilunpatient.config.AppConfig;
import com.yuexun.beilunpatient.ui.bean.BaseListEntity;
import com.yuexun.beilunpatient.ui.login.bean.UserDtlBean;
import com.yuexun.beilunpatient.ui.login.model.impl.LoginModel;
import com.yuexun.beilunpatient.ui.login.presenter.impl.UserDtlPresenter;
import com.yuexun.beilunpatient.ui.login.ui.LoginActivity;
import com.yuexun.beilunpatient.ui.login.ui.view.IUserDtlView;
import com.yuexun.beilunpatient.ui.my.model.impl.UpdateUsrMsgModel;
import com.yuexun.beilunpatient.ui.my.presenter.impl.UpdateUsrMsgPresenter;
import com.yuexun.beilunpatient.ui.my.ui.view.IUpdateUsrMsgView;
import com.yuexun.beilunpatient.widget.ActionSheetDialog;

import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by wangt on 2018/3/22.
 */

public class Act_Personal extends BaseKJActivity implements IUserDtlView, IUpdateUsrMsgView {
    @Bind(R.id.tv_top_name)
    TextView tvName;
    @Bind(R.id.tv_top_idcard)
    TextView tvIdcard;
    @Bind(R.id.tv_card)
    TextView tvCard;
    @Bind(R.id.tv_phone)
    TextView tvPhone;
    @Bind(R.id.tv_sex)
    TextView tvSex;
    @Bind(R.id.tv_birthday)
    TextView tvBirthday;
    @Bind(R.id.tv_marriage)
    TextView tvMarriage;
    @Bind(R.id.tv_job)
    TextView tvJob;
    @Bind(R.id.tv_height)
    TextView tvHeight;
    @Bind(R.id.tv_weight)
    TextView tvWeight;
    @Bind(R.id.tv_blood_1)
    TextView tvBlood_1;
    @Bind(R.id.tv_blood_2)
    TextView tvBlood_2;
    @Bind(R.id.tv_allergy)
    TextView tvAllergy;
    @Bind(R.id.tv_medical_history)
    TextView tvMedicalHistory;
    @Bind(R.id.tv_email)
    TextView tvEmail;
    @Bind(R.id.tv_address)
    TextView tvAddress;
    @Bind(R.id.tv_zip_code)
    TextView tvZipCode;

    UserDtlPresenter presenter;
    UpdateUsrMsgPresenter UpdateUsrMsgPresenter;
    UserDtlBean userDtlBean;

    @Override
    public void setRootView() {
        super.setRootView();
        setContentView(R.layout.act_personal);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        presenter = new UserDtlPresenter(new LoginModel(), this);
        UpdateUsrMsgPresenter = new UpdateUsrMsgPresenter(new UpdateUsrMsgModel(), this);

        loadUsrInfo();

        tvName.setText(PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.USER_NAME));
        tvIdcard.setText(PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.ID_CARD));
    }

    /**
     * 获取用户详情
     */
    private void loadUsrInfo(){
        Map<String, String> params = new HashMap<>();
        params.put("patientId", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.PATIENT_ID));
        params.put("sessionUuidPatient", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
        presenter.loadPatientInfoDetail(params);
    }

    /**
     * 更新用户信息
     */
    private void updateUsrMsg(){
        Map<String, String> params = new HashMap<>();
        params.put("patientId", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.PATIENT_ID));
        params.put("sessionUuidPatient", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
        params.put("postcode", userDtlBean.getPostcode());
        params.put("address", userDtlBean.getAddress());
        params.put("familyMedicalHistory", userDtlBean.getFamilyMedicalHistory());
        params.put("allergyHistory", userDtlBean.getAllergyHistory());
        params.put("RHbloodType", userDtlBean.getRhbloodType());
        params.put("ABObloodType", userDtlBean.getABObloodType());
        params.put("weight", userDtlBean.getWeight());
        params.put("height", userDtlBean.getHeight());
        params.put("profession", userDtlBean.getProfession());
        params.put("maritalStatus", userDtlBean.getMaritalStatus() + "");
        params.put("picUuid", userDtlBean.getPicUuid());
        params.put("birthday", userDtlBean.getBirthday());
        params.put("sex", userDtlBean.getSex() + "");
        params.put("email", userDtlBean.getEmail());
        UpdateUsrMsgPresenter.updateOaAccount(params);
    }

    @Override
    @OnClick({R.id.title_back, R.id.my_out, R.id.ll_marriage, R.id.ll_job, R.id.ll_sex, R.id.ll_blood_1, R.id.ll_blood_2,
            R.id.ll_height, R.id.ll_weight, R.id.ll_allergy, R.id.ll_medical_history, R.id.ll_email, R.id.ll_address,
            R.id.ll_zip_code})
    public void onClick(View view) {
        Bundle bundle  = new Bundle();
        Intent intent = new Intent(aty, Act_Personal_Edit.class);
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
            case R.id.my_out:
                PreferenceHelper.write(aty, AppConfig.USER_INFO, AppConfig.ISLOGIN, false);
                Intent intent1 = new Intent(aty, LoginActivity.class);
                startActivity(intent1);
                ExitApplication.getInstance().exit();

                AppContext appContext = (AppContext) aty.getApplicationContext();
                appContext.custom = "";
                appContext.mPushAgent.deleteAlias(PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.PATIENT_ID),
                        "PATIENT_ID", new UTrack.ICallBack() {
                            @Override
                            public void onMessage(boolean isSuccess, String message) {

                            }
                        });
                break;
            case R.id.ll_sex:
                new ActionSheetDialog(aty)
                        .builder()
                        .setTitle("性别")
                        .setCancelable(false)
                        .setCanceledOnTouchOutside(false)
                        .addSheetItem("男", ActionSheetDialog.SheetItemColor.Blue,
                                new ActionSheetDialog.OnSheetItemClickListener() {
                                    @Override
                                    public void onClick(int which) {
                                        tvSex.setText("男");
                                        userDtlBean.setSex(1);
                                        updateUsrMsg();

                                    }
                                })
                        .addSheetItem("女", ActionSheetDialog.SheetItemColor.Blue,
                                new ActionSheetDialog.OnSheetItemClickListener() {
                                    @Override
                                    public void onClick(int which) {
                                        tvSex.setText("女");
                                        userDtlBean.setSex(2);
                                        updateUsrMsg();
                                    }
                                }).show();
                break;
            case R.id.ll_blood_1:
                new ActionSheetDialog(aty)
                        .builder()
                        .setTitle("ABO血型")
                        .setCancelable(false)
                        .setCanceledOnTouchOutside(false)
                        .addSheetItem("O型", ActionSheetDialog.SheetItemColor.Blue,
                                new ActionSheetDialog.OnSheetItemClickListener() {
                                    @Override
                                    public void onClick(int which) {
                                        tvBlood_1.setText("O型");
                                        userDtlBean.setABObloodType("O型");
                                        updateUsrMsg();

                                    }
                                })
                        .addSheetItem("A型", ActionSheetDialog.SheetItemColor.Blue,
                                new ActionSheetDialog.OnSheetItemClickListener() {
                                    @Override
                                    public void onClick(int which) {
                                        tvBlood_1.setText("A型");
                                        userDtlBean.setABObloodType("A型");
                                        updateUsrMsg();
                                    }
                                })
                        .addSheetItem("B型", ActionSheetDialog.SheetItemColor.Blue,
                                new ActionSheetDialog.OnSheetItemClickListener() {
                                    @Override
                                    public void onClick(int which) {
                                        tvBlood_1.setText("B型");
                                        userDtlBean.setABObloodType("B型");
                                        updateUsrMsg();
                                    }
                                })
                        .addSheetItem("AB型", ActionSheetDialog.SheetItemColor.Blue,
                                new ActionSheetDialog.OnSheetItemClickListener() {
                                    @Override
                                    public void onClick(int which) {
                                        tvBlood_1.setText("AB型");
                                        userDtlBean.setABObloodType("AB型");
                                        updateUsrMsg();
                                    }
                                }).show();
                break;
            case R.id.ll_blood_2:
                new ActionSheetDialog(aty)
                        .builder()
                        .setTitle("Rh血型")
                        .setCancelable(false)
                        .setCanceledOnTouchOutside(false)
                        .addSheetItem("Rh阴性", ActionSheetDialog.SheetItemColor.Blue,
                                new ActionSheetDialog.OnSheetItemClickListener() {
                                    @Override
                                    public void onClick(int which) {
                                        tvBlood_2.setText("Rh阴性");
                                        userDtlBean.setRhbloodType("Rh阴性");
                                        updateUsrMsg();

                                    }
                                })
                        .addSheetItem("Rh阳性", ActionSheetDialog.SheetItemColor.Blue,
                                new ActionSheetDialog.OnSheetItemClickListener() {
                                    @Override
                                    public void onClick(int which) {
                                        tvBlood_2.setText("Rh阳性");
                                        userDtlBean.setRhbloodType("Rh阳性");
                                        updateUsrMsg();
                                    }
                                }).show();
                break;
            case R.id.ll_marriage:
                new ActionSheetDialog(aty)
                        .builder()
                        .setTitle("婚姻状况")
                        .setCancelable(false)
                        .setCanceledOnTouchOutside(false)
                        .addSheetItem("未婚", ActionSheetDialog.SheetItemColor.Blue,
                                new ActionSheetDialog.OnSheetItemClickListener() {
                                    @Override
                                    public void onClick(int which) {
                                        tvMarriage.setText("未婚");
                                        userDtlBean.setMaritalStatus(2);
                                        updateUsrMsg();

                                    }
                                })
                        .addSheetItem("已婚", ActionSheetDialog.SheetItemColor.Blue,
                                new ActionSheetDialog.OnSheetItemClickListener() {
                                    @Override
                                    public void onClick(int which) {
                                        tvMarriage.setText("已婚");
                                        userDtlBean.setMaritalStatus(1);
                                        updateUsrMsg();
                                    }
                                }).show();
                break;
            case R.id.ll_job:
                bundle.putString("title","职业");
                bundle.putSerializable("userData",userDtlBean);
                intent.putExtras(bundle);
                startActivityForResult(intent, 1);
                break;
            case R.id.ll_height:
                bundle.putString("title","身高");
                bundle.putSerializable("userData",userDtlBean);
                intent.putExtras(bundle);
                startActivityForResult(intent, 1);
                break;
            case R.id.ll_weight:
                bundle.putString("title","体重");
                bundle.putSerializable("userData",userDtlBean);
                intent.putExtras(bundle);
                startActivityForResult(intent, 1);
                break;
            case R.id.ll_allergy:
                bundle.putString("title","过敏史");
                bundle.putSerializable("userData",userDtlBean);
                intent.putExtras(bundle);
                startActivityForResult(intent, 1);
                break;
            case R.id.ll_medical_history:
                bundle.putString("title","家庭病史");
                bundle.putSerializable("userData",userDtlBean);
                intent.putExtras(bundle);
                startActivityForResult(intent, 1);
                break;
            case R.id.ll_email:
                bundle.putString("title","邮箱");
                bundle.putSerializable("userData",userDtlBean);
                intent.putExtras(bundle);
                startActivityForResult(intent, 1);
                break;
            case R.id.ll_address:
                bundle.putString("title","家庭地址");
                bundle.putSerializable("userData",userDtlBean);
                intent.putExtras(bundle);
                startActivityForResult(intent, 1);
                break;
            case R.id.ll_zip_code:
                bundle.putString("title","邮编");
                bundle.putSerializable("userData",userDtlBean);
                intent.putExtras(bundle);
                startActivityForResult(intent, 1);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case 0x001:
                userDtlBean = (UserDtlBean) data.getExtras().getSerializable("userData");
                updateUsrMsg();
                break;
        }
    }

    @Override
    public void loadPatientInfoDetail(BaseListEntity<UserDtlBean> entity) {
        if(entity.getStatus().equals("200")){
            userDtlBean = entity.getDatas().get(0);
            tvCard.setText(userDtlBean.getHealthNo());
            tvPhone.setText(userDtlBean.getMobile());
            if(userDtlBean.getSex() == 1){
                tvSex.setText("男");
            }else if(userDtlBean.getSex() == 2){
                tvSex.setText("女");
            }
            tvBirthday.setText(userDtlBean.getBirthday());
            if(userDtlBean.getMaritalStatus() == 1){
                tvMarriage.setText("已婚");
            }else if(userDtlBean.getSex() == 2){
                tvMarriage.setText("未婚");
            }
            tvJob.setText(userDtlBean.getProfession());
            tvHeight.setText(userDtlBean.getHeight());
            tvWeight.setText(userDtlBean.getWeight());
            tvBlood_1.setText(userDtlBean.getABObloodType());
            tvBlood_2.setText(userDtlBean.getRhbloodType());
            tvAllergy.setText(userDtlBean.getAllergyHistory());
            tvMedicalHistory.setText(userDtlBean.getFamilyMedicalHistory());
            tvEmail.setText(userDtlBean.getEmail());
            tvAddress.setText(userDtlBean.getAddress());
            tvZipCode.setText(userDtlBean.getPostcode());
        }
    }

    @Override
    public void updateOaAccount(BaseListEntity baseBean) {
        if(baseBean.getStatus().equals("200")){
            loadUsrInfo();
        }
    }
}
